package com.hh.releasenode.config;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.AutoConfigurationPackages;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.bind.annotation.RequestMapping;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Parameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.paths.RelativePathProvider;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import javax.servlet.ServletContext;
import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName SwaggerConfig
 * @Description swagger配置类
 * @Author KunxianLi
 * Date 2020/5/28 17:01
 * Version 1.0
 **/
@Configuration
@EnableSwagger2
//@ConditionalOnProperty(name ={"enabled"} ,prefix = "swagger",havingValue = "true",matchIfMissing = true)
public class SwaggerConfig {

    @Value("${project.version}")
    private String projectVersion;

    @ConditionalOnWebApplication
    @Bean
//    @ConditionalOnMissingBean(name = "allApi")
    public Docket allApi(BeanFactory beanFactory, ServletContext servletContext) {
        List<Parameter> pars = new ArrayList<Parameter>();
        ParameterBuilder tokenPar = new ParameterBuilder();
        ParameterBuilder    signPar = new ParameterBuilder();
        ParameterBuilder uniqueIdPar = new ParameterBuilder();
        ParameterBuilder requestTimePar = new ParameterBuilder();
        tokenPar.name("token").description("身份令牌").modelRef(new ModelRef("string"))
                .parameterType("header").required(false);
        signPar.name("sign").description("签名").modelRef(new ModelRef("string"))
                .parameterType("header").required(false);
        uniqueIdPar.name("uniqueId").description("唯一标识").modelRef(new ModelRef("string"))
                .parameterType("header").required(false);
        requestTimePar.name("requestTime").description("请求时间").modelRef(new ModelRef("string"))
                .parameterType("header").required(false);
        pars.add(tokenPar.build());
        pars.add(signPar.build());
        pars.add(uniqueIdPar.build());
        pars.add(requestTimePar.build());

        List<String> packages = AutoConfigurationPackages.get(beanFactory);
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .pathProvider(new RelativePathProvider(servletContext))
                .select().apis(input -> {
                    String name = input.declaringClass().getPackage().getName();
                    for (String pack : packages) {
                        if (name.startsWith(pack)) {
                            return true;
                        }
                    }
                    return false;
                }).build()
                .globalOperationParameters(pars);
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder().title("发版releasenote").description("发版releasenote")
                .version(projectVersion)
                .build();
    }
}

